package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.businessService.AdNotifyService;
import com.ktwlsoft.archivemanage.domain.dao.entity.AdNotify;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.adNotify.AdUpOrDownRequest;
import com.ktwlsoft.archivemanage.payload.adNotify.AddAdNotifyRequest;
import com.ktwlsoft.archivemanage.payload.adNotify.EditAdNotifyRequest;
import com.ktwlsoft.archivemanage.service.security.UserAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 通知管理
 */
@Api(description = "管理端PC业务通知")
@RestController
@RequestMapping("/adNotify")
public class AdNotifyController {

    private final AdNotifyService adNotifyService;

    public AdNotifyController(AdNotifyService adNotifyService) {
        this.adNotifyService = adNotifyService;
    }

    @ApiOperation("添加通知")
    @PostMapping("/addNotify")
    @UserAuthenticate(actionCode = "NOTIFICATION_ADD")
    public ResponseEntity<?> addNotify(@RequestBody AddAdNotifyRequest request) {
        adNotifyService.save(request);
        return ResponseEntity.ok(Result.ok(request.toString()));
    }

    //edit notify and add updater and update time.
    @ApiOperation("更新通知")
    @PutMapping("/editNotify")
    @UserAuthenticate(actionCode = "NOTIFICATION_UPDATE")
    public ResponseEntity<?> editNotify(@RequestBody EditAdNotifyRequest request) {
        adNotifyService.edit(request);
        return ResponseEntity.ok(Result.ok(request.toString()));
    }


    @ApiOperation("查看通知")
    @GetMapping("/getNotify")
    @UserAuthenticate(actionCode = "NOTIFICATION_DETAIL", skip = true)
    public ResponseEntity<?> getNotify(@ApiParam("通知id") @RequestParam Long id) {
        return ResponseEntity.ok(Result.ok(adNotifyService.getByIdFromManager(id)));
    }

    @ApiOperation("删除通知")
    @DeleteMapping("/deleteNotify")
    @UserAuthenticate(actionCode = "NOTIFICATION_DELETE")
    public ResponseEntity<?> deleteNotify(@ApiParam("通知id") @RequestParam Long id) {
        return ResponseEntity.ok(Result.ok(adNotifyService.deleteById(id)));
    }

    @ApiOperation("上架/下架")
    @PutMapping("/upOrDown")
    @UserAuthenticate(actionCode = "NOTIFICATION_UP_DOWN")
    public ResponseEntity<?> upOrDown(@RequestBody AdUpOrDownRequest request) {
        return ResponseEntity.ok(Result.ok(adNotifyService.upOrDownById(request.getId(), request.getState(), request.getShown())));
    }

    @ApiOperation("查询列表")
    @GetMapping("/getAll")
    @UserAuthenticate(actionCode = "NOTIFICATION_LIST", skip = true)
    public ResponseEntity<?> getAll(
            @ApiParam("状态") @RequestParam(required = false) AdNotify.State state,
            @ApiParam("操作人") @RequestParam(required = false) String operatorId,
            @ApiParam("操作时间起") @RequestParam(required = false) String operateFrom,
            @ApiParam("操作时间止") @RequestParam(required = false) String operateTo,
            @ApiParam("关键字") @RequestParam(required = false) String keywords,
            @ApiParam("pn") @RequestParam Integer pn,
            @ApiParam("pageSize") @RequestParam Integer pageSize
            ){
        return ResponseEntity.ok(Result.ok(adNotifyService.findAll(pn, pageSize, state, operateFrom, operateTo, operatorId, keywords)));
    }

}
